package com.epalmpay.service.app.impl;

import com.alibaba.fastjson.JSON;
import com.epalmpay.dto.mall.MallGoodInDTO;
import com.epalmpay.dto.mall.MallGoodListOutDTO;
import com.epalmpay.dto.mall.MallGoodOutDTO;
import com.epalmpay.dto.mall.MallGoodQueryDTO;
import com.epalmpay.dto.order.MallOrderQueryDTO;
import com.epalmpay.dto.order.OrderGoodOutDTO;
import com.epalmpay.entity.*;
import com.epalmpay.mapper.MallEntityGoodMapper;
import com.epalmpay.mapper.MallGoodMapper;
import com.epalmpay.mapper.OrderGoodItemMapper;
import com.epalmpay.mapper.OrderGoodMapper;
import com.epalmpay.service.app.IAppShopMallService;
import com.epalmpay.status.Rescode;
import com.epalmpay.util.DateUtil;
import com.epalmpay.util.PropertyCopyUtil;
import com.epalmpay.util.Response;
import com.epalmpay.util.Tools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class AppShopMallServiceImpl implements IAppShopMallService {

    @Resource
    private OrderGoodMapper orderGoodMapper;
    @Resource
    private OrderGoodItemMapper orderGoodItemMapper;
    @Resource
    private MallEntityGoodMapper mallEntityGoodMapper;

    @Resource
    private MallGoodMapper mallGoodMapper;

    @Override
    public Map<String, Long> shopOrderList(Long shopId) {
        return orderGoodMapper.countShopOrder(shopId);
    }

    @Override
    public List<OrderGoodOutDTO> listMallOrder(MallOrderQueryDTO mallOrderQueryDTO) throws ParseException {
        OrderGood orderGood = new OrderGood();
        orderGood.setGroupId(mallOrderQueryDTO.getGroupId());
        orderGood.setOrderNo(mallOrderQueryDTO.getOrderNumOrPhone());
        orderGood.setStatus(mallOrderQueryDTO.getStatus());
        orderGood.setShopId(mallOrderQueryDTO.getShopId());

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStart = mallOrderQueryDTO.getDateStart();
        String dateEnd = mallOrderQueryDTO.getDateEnd();
        if(!Tools.isEmpty(dateStart)) {
            Date dateStartDate = dateFormat.parse(dateStart);
            orderGood.setDateStart(dateStartDate);
        }
        if(!Tools.isEmpty(dateEnd)) {
            Date dateEndDate = dateFormat.parse(dateEnd);
            orderGood.setDateEnd(DateUtil.getDateNext(dateEndDate, 1));
        }

        Integer pageSize = mallOrderQueryDTO.getPageSize();
        Integer pageStart = (mallOrderQueryDTO.getPageNo() - 1) * pageSize;

        List<OrderGoodOutDTO> orderGoodOutDTOList = orderGoodMapper.listShopOrderGood(orderGood, pageStart, pageSize);
        for(int i = 0; i < orderGoodOutDTOList.size(); i++) {
            OrderGoodOutDTO orderGoodOutDTO = orderGoodOutDTOList.get(i);
            if(orderGoodOutDTO.getExpressDetail() != null) {
                orderGoodOutDTO.setExpressOutDTO(JSON.parseObject(orderGoodOutDTO.getExpressDetail(), ExpressOutDTO.class));
                orderGoodOutDTO.setExpressDetail(null);
            }
            List<OrderGoodItem> orderGoodItemList = orderGoodItemMapper.listByOrderId(orderGoodOutDTO.getId());
            orderGoodOutDTO.setOrderGoodItemList(orderGoodItemList);
        }

        return orderGoodOutDTOList;
    }

    @Override
    public MallGoodListOutDTO listShopEntityGood(MallGoodQueryDTO mallGoodQueryDTO, Integer mode,Integer orderType) throws Exception {
        MallGoodListOutDTO mallGoodListOutDTO = new MallGoodListOutDTO();

        MallEntityGood mallEntityGood = new MallEntityGood();
        mallEntityGood.setName(mallGoodQueryDTO.getName());
        mallEntityGood.setCategoryFirstId(mallGoodQueryDTO.getCategoryFirstId());
        mallEntityGood.setCategorySecondId(mallGoodQueryDTO.getCategorySecondId());
        mallEntityGood.setCheckStatus((int)(byte)mallGoodQueryDTO.getAuditStatus());
        mallEntityGood.setIsLaunch(mallGoodQueryDTO.getIsLaunch());
        mallEntityGood.setCategoryFirstId(mallGoodQueryDTO.getCategoryFirstId());
        mallEntityGood.setCategorySecondId(mallGoodQueryDTO.getCategorySecondId());
        mallEntityGood.setName(mallGoodQueryDTO.getName());
        mallEntityGood.setShopId(mallGoodQueryDTO.getShopId());

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStart = mallGoodQueryDTO.getDateStart();
        String dateEnd = mallGoodQueryDTO.getDateEnd();
        if(!Tools.isEmpty(dateStart)) {
            Date dateStartDate = dateFormat.parse(dateStart);
            mallEntityGood.setDateStart(dateStartDate);
        }
        if(!Tools.isEmpty(dateEnd)) {
            Date dateEndDate = dateFormat.parse(dateEnd);
            mallEntityGood.setDateEnd(DateUtil.getDateNext(dateEndDate, 1));
        }

        Integer pageSize = mallGoodQueryDTO.getPageSize();
        Integer pageStart = (mallGoodQueryDTO.getPageNo() - 1) * pageSize;

        List<MallEntityGood> mallEntityGoodList = mallEntityGoodMapper.listShopMallEntityGood(mallEntityGood, pageStart, pageSize, mode, orderType);

        List<MallGoodOutDTO> mallGoodOutDTOList = new ArrayList<>();
        for(int i = 0; i < mallEntityGoodList.size(); i++) {
            MallGoodOutDTO mallGoodOutDTO = new MallGoodOutDTO();
            PropertyCopyUtil.copyProperties(mallEntityGoodList.get(i), mallGoodOutDTO);
            mallGoodOutDTOList.add(mallGoodOutDTO);
        }

        Map<String, Long> count = mallEntityGoodMapper.countByShopId(mallGoodQueryDTO.getShopId());

        mallGoodListOutDTO.setCount(count);
        mallGoodListOutDTO.setMallGoodOutDTOList(mallGoodOutDTOList);
        mallGoodListOutDTO.setErrorCode(Rescode.SUCCESS);

        return mallGoodListOutDTO;
    }

    @Override
    public MallGoodListOutDTO listShopMallGood(MallGoodQueryDTO mallGoodQueryDTO, Integer mode, Integer orderType) throws Exception {
        MallGoodListOutDTO mallGoodListOutDTO = new MallGoodListOutDTO();

        MallGood mallGood = new MallGood();
        mallGood.setName(mallGoodQueryDTO.getName());
        mallGood.setAuditStatus((byte)mallGoodQueryDTO.getAuditStatus());
        mallGood.setCategoryFirstId(mallGoodQueryDTO.getCategoryFirstId());
        mallGood.setCategorySecondId(mallGoodQueryDTO.getCategorySecondId());
        mallGood.setIsLaunch(mallGoodQueryDTO.getIsLaunch());
        mallGood.setCategoryFirstId(mallGoodQueryDTO.getCategoryFirstId());
        mallGood.setCategorySecondId(mallGoodQueryDTO.getCategorySecondId());
        mallGood.setMshopId(mallGoodQueryDTO.getShopId());

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStart = mallGoodQueryDTO.getDateStart();
        String dateEnd = mallGoodQueryDTO.getDateEnd();
        if(!Tools.isEmpty(dateStart)) {
            Date dateStartDate = dateFormat.parse(dateStart);
            mallGood.setDateStart(dateStartDate);
        }
        if(!Tools.isEmpty(dateEnd)) {
            Date dateEndDate = dateFormat.parse(dateEnd);
            mallGood.setDateEnd(DateUtil.getDateNext(dateEndDate, 1));
        }

        Integer pageSize = mallGoodQueryDTO.getPageSize();
        Integer pageStart = (mallGoodQueryDTO.getPageNo() - 1) * pageSize;

        List<MallGood> mallGoodList = mallGoodMapper.listShopMallGood(mallGood, pageStart, pageSize, mode, orderType);

        List<MallGoodOutDTO> mallGoodOutDTOList = new ArrayList<>();
        for(int i = 0; i < mallGoodList.size(); i++) {
            MallGoodOutDTO mallGoodOutDTO = new MallGoodOutDTO();
            PropertyCopyUtil.copyProperties(mallGoodList.get(i), mallGoodOutDTO);
            mallGoodOutDTOList.add(mallGoodOutDTO);
        }

        Map<String, Long> count = mallGoodMapper.countMallGoodByShopId(mallGoodQueryDTO.getShopId());

        mallGoodListOutDTO.setCount(count);
        mallGoodListOutDTO.setMallGoodOutDTOList(mallGoodOutDTOList);
        mallGoodListOutDTO.setErrorCode(Rescode.SUCCESS);

        return mallGoodListOutDTO;
    }



    @Override
    public Response shopMallGoodManipulation(MallGoodInDTO mallGoodInDTO) {

        if(mallGoodInDTO.getGoodType()==1){
            MallEntityGood mallEntityGood = new MallEntityGood();
            mallEntityGood.setId(mallGoodInDTO.getId());
            mallEntityGood.setIsLaunch(mallGoodInDTO.getIsLaunch());
            mallEntityGood.setStockNumber(mallGoodInDTO.getStockNumber());
            mallEntityGood.setCheckStatus(mallGoodInDTO.getAuditStatus() == null ? null : (int)(byte)mallGoodInDTO.getAuditStatus());

            if(mallGoodInDTO.getIsLaunch() != null && mallGoodInDTO.getIsLaunch() == 1) {
                mallEntityGood.setGmtModified(new Date());
            }

            mallEntityGoodMapper.updateByPrimaryKeySelective(mallEntityGood);
        }else{
            MallGood malGood = new MallGood();
            malGood.setId(mallGoodInDTO.getId());
            malGood.setIsLaunch(mallGoodInDTO.getIsLaunch());
            malGood.setStockNumber(mallGoodInDTO.getStockNumber());
            malGood.setAuditStatus(mallGoodInDTO.getAuditStatus() == null ? null : (byte)mallGoodInDTO.getAuditStatus());

            if(mallGoodInDTO.getIsLaunch() != null && mallGoodInDTO.getIsLaunch() == 1) {
                malGood.setGmtModified(new Date());
            }

            mallGoodMapper.updateByPrimaryKeySelective(malGood);

        }


        return Response.getSuccess("操作成功");
    }

    @Override
    public Response addRemark(MallOrderQueryDTO mallOrderQueryDTO) {
        OrderGood orderGood = new OrderGood();
        orderGood.setId(mallOrderQueryDTO.getId());
        orderGood.setRemark(mallOrderQueryDTO.getRemark());
        orderGoodMapper.updateByPrimaryKeySelective(orderGood);
        return Response.getSuccess("操作成功");
    }



    @Override
    public Response goodDetail(String id, String groupId) {
        Map<String, Object> map = new HashMap<String,Object>();
        MallGoodOutDTO mallGoodOutDTO;


        mallGoodOutDTO = mallGoodMapper.queryMallGoodDetail(Long.parseLong(id),Long.parseLong(groupId));
        if(mallGoodOutDTO ==null){
            return Response.getError("商品信息不存在");
        }
        map.put("picture",mallGoodOutDTO.getPicture());
        map.put("name",mallGoodOutDTO.getName());
        map.put("merchantName",mallGoodOutDTO.getMerchantName());
        map.put("isAnytimeRefund",mallGoodOutDTO.getAnytimeRefund());
        map.put("isAutoRefund",mallGoodOutDTO.getAutoRefund());
        map.put("saleNum",mallGoodOutDTO.getSaleNum());
        map.put("salePrice",mallGoodOutDTO.getSalePrice());
        map.put("noVipPrice",mallGoodOutDTO.getNoVipPrice());
        map.put("supplyPrice",mallGoodOutDTO.getSupplyPrice());
        map.put("promotionPrice",mallGoodOutDTO.getPromotionPrice());
        map.put("limitNumber",mallGoodOutDTO.getLimitNumber());
        map.put("validDate",mallGoodOutDTO.getValidDate());
        map.put("attribute",mallGoodOutDTO.getAttribute());
        map.put("settlementRate",mallGoodOutDTO.getSettlementRate());
        if(mallGoodOutDTO.getContent() !=null){
            map.put("content",mallGoodOutDTO.getContent());
        }else{
            map.put("content","");
        }

        return Response.getSuccess(map);
    }



}
